﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
using System.IO;
using Wicresoft.UnifyShow.Portal.Extensions;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class BusinessAvailableController : BaseController
    {
        private IBusinessAvailable businessAvailable;
        private IBusinessSystem businessSystem;
        private ISiteBranch siteBranch;
        private IIncident incident;
        private IChange change;
        private IProblem problem;
        private IUser_X user;
        private ISLA sla;

        [InjectionConstructor]
        public BusinessAvailableController(IUser_X user, IBusinessAvailable ibusinessAvailable, IBusinessSystem ibusinessSystem, ISiteBranch isiteBranch, IIncident iincident, IChange ichange, IProblem iproblem, ISLA isla)
        {
            businessAvailable = ibusinessAvailable;
            businessSystem = ibusinessSystem;
            siteBranch = isiteBranch;
            incident = iincident;
            change = ichange;
            problem = iproblem;
            sla = isla;
            this.user = user;
        }
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)businessAvailable).Dispose();
            ((IDisposable)businessSystem).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)incident).Dispose();
            ((IDisposable)change).Dispose();
            ((IDisposable)problem).Dispose();
            ((IDisposable)sla).Dispose();
            ((IDisposable)user).Dispose();
        }

        public ActionResult Index(int businessSystemId)
        {
            base.SetRoles(user);
            int userID = (int)UserID;

            BusinessSystemModel model = businessSystem.GetBusinessSystemByID(businessSystemId);

            if (!UserSiteIDs.Contains(model.SiteID)) return new HttpStatusCodeResult(403);

            if (model != null)
            {

                IList<SiteBranchModel> sites = siteBranch.GetSiteALLBranch();

                var sitesContainsTheBS = businessSystem.GetSiteIDsContainsTheBusinessSystem(model.Name);

                var bsIDCOntrains = sitesContainsTheBS.Where(o => UserSiteIDs.Contains(o.SiteID));

                IList<DeviceAndAlertCountModel> alertCount = new List<DeviceAndAlertCountModel>();
                IList<SiteBranchAndWorkListModel> incidentList = new List<SiteBranchAndWorkListModel>();
                IList<SiteBranchAndWorkListModel> changeList = new List<SiteBranchAndWorkListModel>();
                IList<SiteBranchAndWorkListModel> problemList = new List<SiteBranchAndWorkListModel>();

                foreach (var item in bsIDCOntrains)
                {
                    int bdID = int.Parse(item.ID.ToString());
                    alertCount = alertCount.Concat(businessSystem.GetSiteAlertCountByBSID(bdID)).ToList();
                    incidentList = incidentList.Concat(incident.GetIncidentInfoList(null, bdID, null, null)).ToList();
                    changeList = changeList.Concat(change.GetChangeInfoList(null, bdID, null, null)).ToList();
                    problemList = problemList.Concat(problem.GetProblemInfoList(null, bdID, null, null)).ToList();
                }

                List<SiteAndSLADetailModel> slaList = sla.GetSiteAndSLA_BySystemName((decimal)userID, model.Name);
                List<SiteAndSLADetailViewModel> list = new List<SiteAndSLADetailViewModel>();
                foreach (var item in slaList)
                {
                    SiteAndSLADetailViewModel view = new SiteAndSLADetailViewModel();
                    view.ID = item.ID;
                    view.Alias = item.Alias;
                    view.Type = item.Type;
                    view.Status = StatusHelper.SLAStatus(item.Status);
                    view.Title = item.Title;
                    list.Add(view);
                }
                List<SiteAndSLADetailViewModel> slaVList = (from c in list
                                                            where c.Status != ""
                                                            group c by new { c.ID, c.Alias, c.Type, c.Status, c.Title } into item
                                                            select new SiteAndSLADetailViewModel
                                                            {
                                                                ID = item.Key.ID,
                                                                Alias = item.Key.Alias,
                                                                Type = item.Key.Type,
                                                                Status = item.Key.Status,
                                                                Title = item.Key.Title,
                                                                Count = item.Count()
                                                            }).ToList();

                ViewData["AlertCount"] = alertCount;
                ViewData["SitesBS"] = UsersSites.Where(o => sitesContainsTheBS.Select(a => a.SiteID).Contains(o.SiteID)).ToList();
                ViewData["SiteBranchAndWorkList"] = incidentList;
                ViewData["SiteBranchAndWorkListBychange"] = changeList;
                ViewData["SiteBranchAndWorkListByproblem"] = problemList;
                ViewData["SLAS"] = slaVList;
            }

            return View(model);
        }

        public ActionResult ShowBusinessAvailablePic(int businessSystemId)
        {
            DateTime dateEnd = DateTime.Parse(DateTime.Now.ToShortDateString());
            DateTime dateStart = dateEnd.AddDays(-7);

            var data = businessAvailable.GetSystemAvailableByBusinessSystemId(businessSystemId, dateStart, dateEnd);

            Chart colChart = new Chart();
            colChart.PaletteCustomColors = GlobalHelper.OneSet;
            colChart.Size = new System.Drawing.Size(500, 180);

            Series series = new Series();
            series.XValueType = ChartValueType.DateTime;
            series.Name = "可用性";
            series.ChartType = SeriesChartType.Column;
            series.IsValueShownAsLabel = true;

            ChartArea chartArea = new ChartArea();
            chartArea.Name = "Default";
            chartArea.BackColor = Color.White;
            chartArea.AxisX.IsMarginVisible = true;
            chartArea.AxisX.MajorGrid.Enabled = false;
            chartArea.AxisY.MajorGrid.Enabled = false;
            chartArea.AxisY.Maximum = 100;


            foreach (var item in data)
            {
                series.Points.AddXY(item.Timestamp, item.Availability);
            }

            colChart.ChartAreas.Add(chartArea);
            colChart.Series.Add(series);
            
            return new ChartResult(colChart);
        }
    }
}
